//
//  WSUserInfoRequest.m
//  CharonTVProject
//
//  Created by karong on 2023/8/4.
//

#import "WSUserInfoRequest.h"

@implementation WSUserInfoRequest

+ (void)editcharonTV_UserInfoWithField:(NSString *)field stringValue:(NSString *)value success:(charonTV_EmptyBlock)success failed:(Failure)failed {
    NSDictionary * parameters = @{@"field":WSEmptyStr(field),
                                  @"value":WSEmptyStr(value)
    };
    [self editcharonTV_UserInfoWithParameters:parameters success:success failed:failed];
}

+ (void)editcharonTV_UserInfoWithField:(NSString *)field integerValue:(NSInteger)value success:(charonTV_EmptyBlock)success failed:(Failure)failed {
    NSDictionary * parameters = @{@"field":WSEmptyStr(field),
                                  @"value":@(value)
    };
    [self editcharonTV_UserInfoWithParameters:parameters success:success failed:failed];
}

+ (void)editcharonTV_UserInfoWithParameters:(NSDictionary *)parameters success:(charonTV_EmptyBlock)success failed:(Failure)failed {
    NSString * url = API_CHARONTV_USER_EDIT;
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        success();
    } failure:failed];
}

+ (void)uploadcharonTV_Image:(UIImage *)image success:(charonTV_StringBlock)success failed:(Failure)failed {
    NSString *url = API_CHARONTV_UPLOAD_IMAGE;
    NSString *fileStr = [@"Q5y9" md5String];
    NSDictionary * parameters = @{@"path":@"dialog",
                                  @"sign":fileStr
    };
    NSData *data =UIImageJPEGRepresentation(image, 0.6) ;// UIImageJPEGRepresentation([UIImage fixOrientationImage:image], 0.8);
    if (data == nil) {
        data = UIImagePNGRepresentation(image);
    }
    if (!data) {
        return;
    }
    // 設置時間格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *dateString = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg", dateString];
    [self postWithURLString:url parameters:parameters fileDatas:@[data] fileNames:@[fileName] success:^(NSDictionary *responseDict) {
        NSDictionary * date = responseDict[@"data"];
        NSString *imageURLString = date[@"url"];
        if (WSIsNullOrEmpty(imageURLString)) {
            [WSToast simple:@"圖片上傳失敗，請重新選擇"];
        }
        else {
            success(imageURLString);
        }
    } failure:failed];
}
@end
